// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var tools = require("tools");
	var validate = require("validate");
	var js_platform_main = require("./platform_main.js");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var left;
	var pickupId;
	var init = function(page) {
		left = page.params.left || base.getUrlParam("left");
		if(left == null || left == 7){
			left=10;
		}
		$(".left").removeClass("none");
		leftMenu.init(6);
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		js_platform_main.init(0);
		inquireReceivablesPageList();
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		$("#receivablesNotice_list .searchButon").unbind("click").click(function() {
			pageIndex = 1;
			if ($("#receivablesNotice_list .searchStatus").val() == 1) {
				inquireReceivablesPageList1();
			} else {
				inquireReceivablesPageList();
			}
		});
	};

	//查询收款确认-提货信息列表
	var inquireReceivablesPageList = function() {
		var productType = $("#receivablesNotice_list select[name=productType]").val();
		base.fnAjax({
			action: "inquireWaitReceivablesPickupApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				financeNumber:$("#receivablesNotice_list .financeNumber").val(),
				enterpriseName :$("#receivablesNotice_list .enterpriseName").val(),
				bulkstockName: $("#receivablesNotice_list .bulkstockName").val(),
				productType: productType,
				pageSize: pageSize,
				pickupId: $("#receivableses").val().split(',')
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#recievesListTpl").html();
					var html = juicer(tpl, data);
					$("#recievesListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#recievespagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireReceivablesPageList();
						});

					//点击收款
					$("#recievesListBody").on("click", ".details_link", function() {
						var pickupId;
						pickupId = $(this).parents("tr").attr("pickupId");

						router.route("main/receivablesNotice.html?pickupId=" + pickupId + "&left=" + left, true);
					});
					//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeIds, true);
					});
					
					tools.changeWarningColor($("#recievesListBody"), "receiveConfirmCreateDateTime");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//查询收款确认-提货信息列表
	var inquireReceivablesPageList1 = function() {
		var productType = $("#receivablesNotice_list select[name=productType]").val();
		base.fnAjax({
			action: "inquireWaitReceivablesPickupApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				financeNumber:$("#receivablesNotice_list .financeNumber").val(),
				enterpriseName :$("#receivablesNotice_list .enterpriseName").val(),
				bulkstockName: $("#receivablesNotice_list .bulkstockName").val(),
				productType: productType,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#recievesListTpl").html();
					var html = juicer(tpl, data);
					$("#recievesListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#recievespagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireReceivablesPageList1();
						});

					$("#recievesListBody .details_link").html("详情");

					//点击放款
					$("#recievesListBody").on("click", ".details_link", function() {
						var pickupId;
						pickupId = $(this).parents("tr").attr("pickupId");

						router.route("main/receivablesNotice.html?pickupId=" + pickupId + "&left=" + left, true);
					});
					//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeIds, true);
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
			
		});
	};
	return {
		init: init
	};
});